﻿using KZ.Utilities;
using System;
using System.Data;
using System.Windows.Forms;
using KZ.Express.H.ENT;
using KZ.Express.H.Presenter;

namespace KZ.Express.H.Settings.PriceList
{
    public partial class OriginalPriceDetailView : BaseView, IPriceDetailView
    {
        #region Private Variables

        DataTable _tblDOX, _tblWPX;

        #endregion Private Variables

        #region Private Methods

        private void OriginalPriceDetailView_Load(object sender, EventArgs e)
        {
            LoadCarrier();
            LoadZone();
            LoadData();
        }

        private void cmbCarrierID_SelectedValueChanged(object sender, EventArgs e)
        {
            LoadSupplier();
        }

        private void cmbZoneID_SelectedValueChanged(object sender, EventArgs e)
        {
            LoadZoneDetail();
        }

        private void LoadData()
        {
            try
            {
                switch (base.ActionName)
                {
                    case "UpdateWithImport":
                        ((OriginalPriceDetailPresenter)_presenter).LoadEntity<PriceListENT>();
                        ((OriginalPriceDetailPresenter)_presenter).LoadDataTable();
                        break;
                }
            }
            catch (BusinessLogicException ex)
            {
                ShowMessageBoxError(ex.Message);
            }
        }

        private void LoadCarrier()
        {
            try
            {
                int? carrierID = Utility.IntNParse(cmbCarrierID.SelectedValue);
                cmbCarrierID.SelectedValueChanged -= cmbCarrierID_SelectedValueChanged;

                cmbCarrierID.ValueMember = "CarrierID";
                cmbCarrierID.DisplayMember = "CarrierName";
                DataTable dt = ((OriginalPriceDetailPresenter)_presenter).LoadDataTable<CarrierENT>(null);
                DataRow row = dt.NewRow();
                dt.Rows.InsertAt(row, 0);
                cmbCarrierID.DataSource = dt;

                if (carrierID.HasValue)
                    cmbCarrierID.SelectedValue = carrierID;
                else
                    cmbCarrierID.SelectedValue = DBNull.Value;
            }
            catch (BusinessLogicException ex)
            {
                ShowMessageBoxError(ex.Message);
            }
            finally
            {
                cmbCarrierID.SelectedValueChanged += cmbCarrierID_SelectedValueChanged;
            }
        }

        private void LoadSupplier()
        {
            try
            {
                int? supplierID = Utility.IntNParse(cmbSupplierID.SelectedValue);

                SearchCriteria search = new SearchCriteria();
                search.Add("CarrierID", Utility.IntParse(cmbCarrierID.SelectedValue));
                cmbSupplierID.ValueMember = "SupplierID";
                cmbSupplierID.DisplayMember = "SupplierName";
                DataTable dt = ((OriginalPriceDetailPresenter)_presenter).LoadDataTable<SupplierENT>(search);
                DataRow row = dt.NewRow();
                dt.Rows.InsertAt(row, 0);
                cmbSupplierID.DataSource = dt;

                if (supplierID.HasValue)
                    cmbSupplierID.SelectedValue = supplierID;
                else
                    cmbSupplierID.SelectedValue = DBNull.Value;
            }
            catch (BusinessLogicException ex)
            {
                ShowMessageBoxError(ex.Message);
            }
        }

        private void LoadZone()
        {
            try
            {
                int? zoneID = Utility.IntNParse(cmbZoneID.SelectedValue);

                cmbZoneID.SelectedValueChanged -= cmbZoneID_SelectedValueChanged;
                cmbZoneID.ValueMember = "ZoneID";
                cmbZoneID.DisplayMember = "ZoneCode";
                DataTable dt = ((OriginalPriceDetailPresenter)_presenter).LoadDataTable<ZoneENT>(null);
                DataRow row = dt.NewRow();
                dt.Rows.InsertAt(row, 0);
                cmbZoneID.DataSource = dt;

                if (zoneID.HasValue)
                    cmbZoneID.SelectedValue = zoneID;
                else
                    cmbZoneID.SelectedValue = DBNull.Value;
            }
            catch (BusinessLogicException ex)
            {
                ShowMessageBoxError(ex.Message);
            }
            finally
            {
                cmbZoneID.SelectedValueChanged += cmbZoneID_SelectedValueChanged;
            }
        }

        private void LoadZoneDetail()
        {
            try
            {
                ((OriginalPriceDetailPresenter)_presenter).LoadZoneDetail(Utility.IntParse(cmbZoneID.SelectedValue));
            }
            catch (BusinessLogicException ex)
            {
                ShowMessageBoxError(ex.Message);
            }
        }

        #endregion Private Methods

        #region Constructors

        public OriginalPriceDetailView()
        {
            InitializeComponent();
            _presenter = new OriginalPriceDetailPresenter(this);

            base.ObjectID = (int)Objects.OriginalPriceList;
            Global.SetDoubleBuffered(dtgDOXDetail);
            Global.SetDoubleBuffered(dtgWPXDetail);
        }

        #endregion Constructors

        #region Public Properties

        [Search]
        public int PriceListID
        {
            get { return base.EntityID; }
        }

        public DataTable ZoneTypes
        {
            set
            {
                for (int i = colTemp.Index - 1; i > colDOXWeight.Index; i--)
                {
                    dtgDOXDetail.Columns.RemoveAt(i);
                    dtgWPXDetail.Columns.RemoveAt(i);
                }
                _tblDOX = new DataTable();
                _tblDOX.Columns.Add("Number", typeof(int));
                _tblDOX.Columns.Add("Weight", typeof(string));

                _tblWPX = new DataTable();
                _tblWPX.Columns.Add("Number", typeof(int));
                _tblWPX.Columns.Add("Weight", typeof(string));

                int index = 1;
                int rowCount = value.Rows.Count;
                for (int i = rowCount - 1; i >= 0; i--)
                {
                    DataGridViewColumn colDOX = new DataGridViewColumn(dtgDOXDetail.Columns[colDOXWeight.Index].CellTemplate);
                    colDOX.Name = "colDOX" + value.Rows[i]["Zone"];
                    colDOX.HeaderText = value.Rows[i]["Zone"].ToString();
                    colDOX.DataPropertyName = value.Rows[i]["Zone"].ToString();
                    colDOX.Width = 60;
                    colDOX.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                    colDOX.DefaultCellStyle.Format = "N2";

                    _tblDOX.Columns.Add(colDOX.DataPropertyName, typeof(decimal));
                    dtgDOXDetail.Columns.Insert(colDOXWeight.Index + index, colDOX);

                    DataGridViewColumn colWPX = new DataGridViewColumn(dtgWPXDetail.Columns[colWPXWeight.Index].CellTemplate);
                    colWPX.Name = "colWPX" + value.Rows[i]["Zone"];
                    colWPX.HeaderText = value.Rows[i]["Zone"].ToString();
                    colWPX.DataPropertyName = value.Rows[i]["Zone"].ToString();
                    colWPX.Width = 60;
                    colWPX.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                    colWPX.DefaultCellStyle.Format = "N2";

                    _tblWPX.Columns.Add(colWPX.DataPropertyName, typeof(decimal));
                    dtgWPXDetail.Columns.Insert(colWPXWeight.Index + index, colWPX);
                }
            }
        }

        public DataTable TblDOX
        {
            get { return _tblDOX; }
            set
            {
                _tblDOX = value;
                bdsDOX.DataSource = value;
                Global.GenerateNumber(ref value);
            }
        }

        public DataTable TblWPX
        {
            get { return _tblWPX; }
            set
            {
                _tblWPX = value;
                bdsWPX.DataSource = value;
                Global.GenerateNumber(ref value);
            }
        }

        public override BaseENT Entity
        {
            get
            {
                PriceListENT priceList = new PriceListENT();
                priceList.PriceListID = base.EntityID;
                priceList.PriceListCode = txtPriceListCode.Text;
                priceList.PriceListName = txtPriceListName.Text;
                priceList.CarrierID = Utility.IntNParse(cmbCarrierID.SelectedValue);
                priceList.ZoneID = Utility.IntNParse(cmbZoneID.SelectedValue);
                priceList.SupplierID = Utility.IntNParse(cmbSupplierID.SelectedValue);
                priceList.EffectedDate = dtpEffectedDate.Value.Date;
                priceList.ExpiredDate = dtpExpiredDate.Value.Date;
                priceList.IsOriginal = true;

                priceList.CreatedBy = UserENT._USERID;
                priceList.UpdatedBy = UserENT._USERID;

                priceList.ValidateData();
                if (_tblDOX.Rows.Count == 0 && _tblWPX.Rows.Count == 0)
                    throw new BusinessLogicException("Bạn phải nhập giá");

                return priceList;
            }
            set
            {
                PriceListENT priceList = value == null ? new PriceListENT() : value as PriceListENT;

                txtPriceListCode.Text = priceList.PriceListCode;
                txtPriceListName.Text = priceList.PriceListName;
                cmbCarrierID.SelectedValue = Utility.IntParse(priceList.CarrierID);
                cmbZoneID.SelectedValue = Utility.IntParse(priceList.ZoneID);
                cmbSupplierID.SelectedValue = Utility.IntParse(priceList.SupplierID);

                dtpEffectedDate.Value = Utility.DateTimeParse(priceList.EffectedDate == DateTime.MinValue ? DateTime.Today : priceList.EffectedDate);
                dtpExpiredDate.Value = Utility.DateTimeParse(priceList.ExpiredDate == DateTime.MinValue ? DateTime.Today : priceList.ExpiredDate);

                if (value == null)
                {
                    _tblDOX = null;
                    _tblWPX = null;
                }
            }
        }

        #endregion Public Properties

        #region Public Methods

        public override void Import()
        {
            SetMessage(String.Empty);
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                Cursor = Cursors.WaitCursor;

                try
                {
                    bdsDOX.DataSource = null;
                    bdsWPX.DataSource = null;
                    ((OriginalPriceDetailPresenter)_presenter).ImportExcel(ofd.FileName, _tblDOX);
                }
                finally
                {
                    Cursor = Cursors.Default;
                }
            }
        }

        public override void RefreshData()
        {
            LoadZone();
            LoadCarrier();
            LoadSupplier();
        }

        #endregion Public Methods
    }
}